//
//  JSSUserAccont.swift
//  WeiBoItem
//
//  Created by jss on 16/4/19.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit

class JSSUserAccont: NSObject,NSCoding{

    /*
    ({
    "access_token" : "2.00ZOAyNG0Od8au2f306448a004fWBz",
    "expires_in" : 157679999,
    "remind_in" : 157679999,
    "uid" : 5703189943,
    })
    */
   
    //用户接口中必填参数
   
    
    var uid:String?
 
    // 过期时间
    // -[__NSCFNumber length] - unrecognized selector sent to instance 0xb000000000229c43'
    // 代表属性的类型写错啦。把 Int 写成 String 了
    // accessToken的生命周期 --> 求出过期时间
    
//   var expires_in: String?
    //var expires_in:Int = 0
    
    var expires_in: NSTimeInterval = 0 {
        
        didSet {
            
            //计算过期时间
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    // 过期时间 ： 当accessToken获取回来的那一刻开始算起， expires_in 秒后的时间
    var expiresDate:NSDate?
    
    var access_token: String?

    var remind_in: String?
    
     //用户名字
    var name:String?
    //用户头像地址大图 
    var avatar_large:String?
    
    init(dict:[String:AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }

    //解档的方法(解档哪些属性)
    required init?(coder aDecoder: NSCoder) {
        
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        remind_in = aDecoder.decodeObjectForKey("remind_in") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
        //接档过期时间
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        
    }
    
    
    //归档调用的方法(归档哪些属性)
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(remind_in, forKey: "remind_in")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
        //将过期时间归档
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
    }
    
}
